define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template', 'echarts', 'echarts-theme'], function ($, undefined, Backend, Table, Form, Template, Echarts) {

    var Controller = {
        index: function () {
            //这句话在多选项卡统计表时必须存在，否则会导致影响的图表宽度不正确
            $(document).on("click", ".charts-custom a[data-toggle=\"tab\"]", function () {
                var that = this;
                setTimeout(function () {
                    var id = $(that).attr("href");
                    var chart = Echarts.getInstanceByDom($(id)[0]);
                    chart.resize();
                }, 0);
            });

            Table.api.init({
                extend: {
                    index_url: 'data/shopdaytrade/index2',
                    add_url: '',
                    edit_url: '',
                    multi_url: '',
                }
            });


            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'total_amount',
                columns: [
                    [
                        {field: 'id', title: 'ID'},
                        {
                            field: 'shop_name', title: __('店铺'),align:'left', formatter: function (value, row, index, field) {
                                if (value == null) {
                                    return "";
                                }
                                return "<span style='display: block;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;' title='" + row.value + "'>" + value + "</span>";
                            },
                            cellStyle: function (value, row, index, field) {
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width": "100px"
                                    }
                                };
                            }
                        },
                        {field: 'total_amount', title: __('总金额'), operate: false, sortable: true},
                        {field: 'book_amount', title: __('棋牌'), sortable: true},
                        {field: 'tq_amount', title: __('台球'), sortable: true},
                        {
                            field: 'create_time',
                            title: __('时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                        },
                    ]
                ],
            });

            // 为表格绑定事件
            Table.api.bindevent(table);


            // 基于准备好的dom，初始化echarts实例
            var lineChart = Echarts.init(document.getElementById('line-chart'), 'walden');

            // 指定图表的配置项和数据
            $.ajax({
                url: "/api/admin/index/tradeDay",
                type: "POST",
                data: {type: 'amount'},
                dataType: "json",
                success: function (result) {

                    var option = {
                        xAxis: {
                            type: 'category',
                            data: result.data.x
                        },
                        yAxis: {
                            type: 'value'
                        },
                        series: [{
                            data: result.data.y,
                            type: 'line',
                            itemStyle: {normal: {label: {show: true}}}

                        }]
                    };
                    // 使用刚指定的配置项和数据显示图表。
                    lineChart.setOption(option);

                }
            });


            // 基于准备好的dom，初始化echarts实例
            var areaChart = Echarts.init(document.getElementById('area-chart'), 'walden');

            // 指定图表的配置项和数据
            $.ajax({
                url: "/api/admin/index/tradeDay",
                type: "POST",
                data: {type: 'profit'},
                dataType: "json",
                success: function (result) {

                    var option = {
                        xAxis: {
                            type: 'category',
                            data: result.data.x
                        },
                        yAxis: {
                            type: 'value'
                        },
                        series: [{
                            data: result.data.y,
                            type: 'line',
                            itemStyle: {normal: {label: {show: true}}}

                        }]
                    };
                    // 使用刚指定的配置项和数据显示图表。
                    areaChart.setOption(option);

                }
            });


            var barChart = Echarts.init(document.getElementById('bar-chart'), 'walden');


            $.ajax({
                url: "/api/admin/index/regUser",
                type: "POST",
                dataType: "json",
                success: function (result) {

                    option = {
                        legend: {},
                        tooltip: {},
                        dataset: {
                            source: [
                                result.data.x,
                                result.data.y,
                            ]
                        },
                        xAxis: {type: 'category'},
                        yAxis: {},

                        series: [
                            {type: 'bar'},
                            {type: 'bar'},
                            {type: 'bar'}
                        ]
                    };
                    // 使用刚指定的配置项和数据显示图表。
                    barChart.setOption(option);

                }
            });


        }
    };
    return Controller;
});
